const { Counter, Device } = require('../common/mysql.js')
const { Op } = require('sequelize')

module.exports = {
    async getCountAndAll(store_id, request) {
        const { page, pageSize, name, no, status, endBegin, endEnd } = request
        const start = (page - 1) * pageSize
        const condition = {
            store_id: {
                [Op.eq]: store_id
            }
        }
        if (name) {
            condition.name = {
                [Op.like]: `%${name}%`
            }
        }
        if (no) {
            condition.no = {
                [Op.like]: `%${no}%`
            }
        }
        if (status) {
            condition.status = {
                [Op.in]: status
            }
        }
        if (endBegin) {
            condition.end = {
                [Op.gte]: endBegin
            }
        }
        if (endEnd) {
            condition.end = Object.assign({}, condition.end, {
                [Op.lte]: endEnd
            })
        }
        return await Counter.findAndCountAll({
            include: [
                {
                    model: Device
                }
            ],
            where: condition,
            offset: start ? start : 0,
            limit: pageSize ? Number(pageSize) : null,
            distinct: true
        })
    },
    async getByPk(id, store_id) {
        return await Counter.findOne({
            where: {
                id: {
                    [Op.eq]: id
                },
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async getById(id, store_id) {
        return await Counter.findOne({
            include: [
                {
                    model: Device
                }
            ],
            where: {
                id: {
                    [Op.eq]: id
                },
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async insert(counter) {
        return await Counter.create(counter, {
            fields: ['name', 'no', 'begin', 'end', 'status', 'store_id']
        })
    },
    async update(id, store_id, counter, fields) {
        await Counter.update(counter, {
            fields,
            where: {
                id: {
                    [Op.eq]: id
                },
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async deleteById(id) {
        await Counter.destroy({
            where: {
                id: {
                    [Op.eq]: id
                }
            }
        })
    },
    async distribute(id, store_id, device_id) {
        await Counter.update(
            {
                status: 2,
                device_id: device_id
            },
            {
                fields: ['status', 'device_id'],
                where: {
                    id: {
                        [Op.eq]: id
                    },
                    store_id: {
                        [Op.eq]: store_id
                    }
                }
            }
        )
    },
    async exit(id, store_id, remark) {
        await Counter.update(
            {
                status: 9,
                out: new Date(),
                remark: remark
            },
            {
                fields: ['status', 'remark', 'out'],
                where: {
                    id: {
                        [Op.eq]: id
                    },
                    store_id: {
                        [Op.eq]: store_id
                    }
                }
            }
        )
    }
}

// export async function renewal(id, store_id, month) {
//     const counter = await Counter.findOne({
//         where: {
//             id: {
//                 [Op.eq]: id
//             },
//             store_id: {
//                 [Op.eq]: store_id
//             },
//             status: {
//                 [Op.eq]: 2
//             }
//         }
//     })
//     const end = new Date(counter.end)
//     end.setMonth(end.getMonth() + month)
//     counter.end = end
//     await counter.save({
//         fields: ['end']
//     })
// }

// export async function updateStatus(id, store_id, status) {
//     await Counter.update({
//         status: status
//     }, {
//         fields: ['status'],
//         where: {
//             id: {
//                 [Op.eq]: id
//             },
//             store_id: {
//                 [Op.eq]: store_id
//             }
//         }
//     })
// }
